package com.jiazhong.basic.pack0517;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DemoA {
    public static void main(String[] args)
            throws ClassNotFoundException, SQLException {
        // 1. 加载驱动类 Driver
        // Class.forName(Driver.class.getName());
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2. 建立Java同MySQL之间的连接通道
        String url = "jdbc:mysql://localhost:3306/jiazhong_2025_2"; // 路径｜位置
        String user = "root";
        String password = "a123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println("建立了Java同数据库之间的连接通道");
        System.out.println(conn);
        // 手动提交事务
        conn.setAutoCommit(false);
        // 3. 创建一个携带命令的传令官
        String sql = "insert into emp values(null,'马浩伟','人工智能',8051,now(),9999,222,30,1)";
        PreparedStatement ps = conn.prepareStatement(sql);
        // 4. 执行命令
        int row = ps.executeUpdate(); // insert update delete
        System.out.println(row+"行收到了影响");
        // 5. 提交事务
        conn.commit(); // conn.rollback();
        // 5. 关闭数据库连接
        ps.close();
        conn.close();
    }
}
